package org.bird.title;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 类QuestionTow.java的实现描述：有一语句，例如："hello you hello me"，请找出重复词语并按照次数排序输出
 * 
 * @author liangjf 2017年6月21日 下午1:55:51
 */
public class QuestionTow {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str = "hello are you is feifei is not hello you hello";
        List<Title> list = new ArrayList<Title>();
        String[] temp1, temp2;
        temp1 = temp2 = str.split(" ");
        for (int i = 0; i < temp1.length; i++) {
            int count = 0;
            for (int j = 0; j < temp2.length; j++) {
                if (temp1[i].equals(temp2[j])) {
                    count++;
                }
            }
            QuestionTow demo = new QuestionTow();
            Title title = demo.new Title(count, temp1[i]);
            list.add(title);
        }
        Collections.sort(list);
        for (Title title : list) {
            System.out.println(String.format("%s->%s", title.getCount(), title.getName()));
        }
    }

    class Title implements Comparable<Title> {

        Integer        count;
        private String name;

        public Title(int count, String name) {
            this.count = count;
            this.name = name;
        }

        /**
         * @return the count
         */
        public int getCount() {
            return count;
        }

        /**
         * @param count the count to set
         */
        public void setCount(int count) {
            this.count = count;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }

        /*
         * (non-Javadoc)
         * @see java.lang.Comparable#compareTo(java.lang.Object)
         */
        public int compareTo(Title o) {
            return this.count.compareTo(o.count);
        }
    }
}
